<template>
  <div style="padding:20px">
    <el-form ref="form" label-width="80px">
      <el-form>
        <el-form-item label="课程标题" label-width="80px">
          <el-input v-model="courseInfo.title"/>
        </el-form-item>
      </el-form>
      <el-form-item label="课程讲师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择活动区域">
          <el-option
            v-for="teacher in teachers"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="课程分类">
        <el-select
          v-model="courseInfo.subjectParentId"
          placeholder="请选择一级分类"
          @change="handleSubjectChange">
          <el-option
            v-for="pSubject in pSubjects"
            :key="pSubject.id"
            :label="pSubject.title"
            :value="pSubject.id"/>
        </el-select>
        <el-select v-model="courseInfo.subjectId" placeholder="请选择二级分类">
          <el-option
            v-for="cSubject in cSubjects"
            :key="cSubject.id"
            :label="cSubject.title"
            :value="cSubject.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="总课时">
        <el-input-number v-model="courseInfo.lessonNum" :min="0" controls-position="right" />
      </el-form-item>
      <el-form-item label="课程简介">
        <tinymce :height="300" v-model="courseInfo.description"/>
      </el-form-item>
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="courseFaceUrl"
          class="avatar-uploader">
          <img v-if="courseInfo.cover" :src="courseInfo.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="课程价格">
        <el-input-number v-model="courseInfo.price" :min="0" controls-position="right" />
      </el-form-item>
      <el-form-item style="text-align:center">
        <el-button type="primary" @click="saveAndCommit">保存并下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacherApi'
import subjectApi from '@/api/subject'
import courseApi from '@/api/course'
import Tinymce from '@/components/Tinymce'

export default {
  components: {
    Tinymce
  },
  data() {
    return {
      teacher: {},
      teachers: [],
      pSubjects: [],
      cSubjects: [],
      courseInfo: {
        subjectId: '',
        teacherId: '',
        subjectParentId: '',
        lessonNum: 0
      },
      courseFaceUrl: 'http://localhost:9110/admin/oss/upload?module=course-pic'
    }
  },
  created() {
    // 判断是否存在courseId,如果有则查询id的对应的course
    if (this.$parent.courseId) {
      courseApi.getCourseById(this.$parent.courseId).then(r => {
        this.courseInfo = r.data.item
      })
    }
    this.getTeacherlist()
    this.getSubjectlist()
  },
  methods: {

  // 获取教师列表
    getTeacherlist() {
      teacherApi.list().then(r => {
        this.teachers = r.data.items
      })
    },
    // 获取章节列表信息
    getSubjectlist() {
      subjectApi.getParents().then(r => {
        this.pSubjects = r.data.parent
      })
    },

    // 二级分类随着一级分类变化
    handleSubjectChange(pSubjectId) {
      this.courseInfo.subjectId = ''
      this.pSubjects.forEach(pSubject => {
        if (pSubject.id === pSubjectId) {
          this.cSubjects = pSubject.children
          return
        }
      })
    },
    // 封面地址保存到对应的数据库，并回显图片
    handleAvatarSuccess(res, file) {
      this.courseInfo.cover = res.data.url
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    update() {
      courseApi.editCourse(this.$parent.courseId, this.courseInfo).then(r => {
        this.$message.success('更新成功')
        this.$parent.active = 1
      })
    },
    save() {
      courseApi.saveAndCommit(this.courseInfo).then(r => {
        this.$parent.courseId = r.data.id
        this.$message.success('保存成功')
        this.$parent.active = 1
      })
    },
    saveAndCommit() {
      if (this.$parent.courseId) {
        this.update()
      } else {
        this.save()
      }
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 365px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width:  365px;
    height: 178px;
    display: block;
  }
</style>
<style>
.tinymce-container {
  position: relative;
  line-height: normal;
}
</style>
